$(function () {
	$.ownStore.removePid()
	var patientId = url('?userid') //获取网址中参数值
	$.ownStore.setPid(patientId)
	//	localStorage.setItem('pid', patientId)         //存储pid
	console.log('pid', $.ownStore.getPid()) //打印存储数据pid
	var staffId, staffName
	if (!document.referrer.match('/doctor/i')) {
		//根据上层网址来源判断缓存
		if (url('?staff_id') && url('?staff_name')) {
			staffId = url('?staff_id')
			$.ownStore.setStaffId(staffId)
			staffName = url('?staff_name')
			$.ownStore.setStaffName(staffName)
		} else {
			staffId = $.ownStore.getStaffId()
			staffName = $.ownStore.getStaffName()
		}

		console.log('set', $.ownStore.getStaffId(), $.ownStore.getStaffName())
	} else {
		$.ownStore.removeStaffName()
		$.ownStore.removeStaffId()
		console.log('remove', $.ownStore.getStaffId(), $.ownStore.getStaffName())
	}

	//tab
	let tabList = [
		{
			img: 'img/index/file.png',
			title: '患者档案',
			//		'desc': '完整详细了解患者基础信息',
			link: 'files',
			linkpath: 'filesIndex.html',
		},
		{
			img: 'img/index/daily.png',
			title: '日常监测',
			//		'desc': '随时随地关注患者打卡动态',
			link: 'daily',
			linkpath: 'dailyIndex.html?userid=' + patientId,
		},
		{
			img: 'img/index/ask.png',
			title: '复诊随访',
			//		'desc': '全方位开展患者复诊随访工作',
			link: 'follow',
			linkpath: 'follow/followRecord.html?from=patient',
		},
		{
			img: 'img/index/questionnaire.png',
			title: '量表评估',
			//		'desc': '多角度评估患者疾病、行为、心理',
			link: 'assess',
			linkpath: 'evalutionIndex.html?userid=' + patientId,
		},
		{
			img: 'img/index/report.png',
			title: '健管报告',
			//		'desc': '详尽了解患者阶段、专项报告',
			link: 'report',
			linkpath: 'report.html',
		},
		{
			img: 'img/index/service.png',
			title: '健管服务',
			//		'desc': '数字化、科学化管理患者',
			link: 'service',
			linkpath: 'hMService.html?userid=' + patientId + '&from=patient',
		},
	]

	//循环添加tab
	$.each(tabList, function (i, v) {
		var html = [
			'<li linkpath=' + v.linkpath + '>',
			'	<div class="tab_list_item">',
			'		<div class="tab_list_item_img">',
			'			<img src="' + v.img + '"/>',
			'		</div>',
			//			'		<div class="tab_list_item_info">',
			'			<p class="tab_list_item_info_title fontsize-ml">' + v.title + '</p>',
			//			'		</div>',
			'	</div>',
			'</li>',
		].join('')
		$(html).appendTo('.tab_list ul')
	})

	//循环添加tab点击跳转
	$('.tab_list ul li').each(function (i) {
		$('.tab_list ul li')
			.eq(i)
			.click(function () {
				if ($(this).attr('linkpath')) {
					window.location.href = $(this).attr('linkpath')
				} else {
					syalert.syopen('alert3')
				}
			})
	})

	//患者信息跳转详情
	$('.basic_info').click(function () {
		//		syalert.syopen('alert3')
		window.location.href = './files/baseInfo.html'
	})

	//-------------------------------接口调用-------------------------------

	//基础信息
	$.apost(
		PATH_INDEXBASICINFO,
		{ patientId: patientId, staffId: staffId, staffName: staffName },
		function (data) {
			console.log('data', data)
			//      	$('.basic_info_sign p').text('连续打卡'+data.checkinDays+'天')
			if (data.patientsImg) {
				$('.basic_info_avater img').attr('src', data.patientsImg)
			}
			if (data.patientsName) {
				$('.basic_info_name').text(data.patientsName)
			} else if (data.patientsNickname) {
				$('.basic_info_name').text(data.patientsNickname)
			} else {
				$('.basic_info_name').text('匿名')
			}

			if (data.serviceName) {
				$('.basic_info_state').show()
			}
			if (data.patientsSex) {
				if (data.patientsSex == 1) {
					$('.basic_info_sex').text('性别：男')
				} else {
					$('.basic_info_sex').text('性别：女')
				}
			}

			if (data.age) {
				$('.basic_info_age').text('年龄：' + data.age + '岁')
			}

			if (data.patientsDiabetisTypeName) {
				$('.basic_info_type').text(
					'糖尿病类型：' + data.patientsDiabetisTypeName
				)
			}
			if (data.patientsConfirmedDate) {
				$('.basic_info_confirmdate').text(
					'确诊日期：' + data.patientsConfirmedDate
				)
			}
			if (data.patientsDesc) {
				$('.basic_info_symtons')
					.text('简要病史：' + data.patientsDesc)
					.width('100%')
			}
		},
		function (e) {
			console.log('error')
		}
	)

	//	//控制目标
	$.apost(
		PATH_TARGET,
		{ patientId: patientId, staffId: staffId, staffName: staffName },
		function (data) {
			console.log('1', data)
			if (data.length != 0) {
				$.each(data, function (i, v) {
					var html = [
						'<li class="flex">',
						'	<div class="index_target_table_info_name" secname="' +
							v.nameSec +
							'">' +
							v.name +
							'</div>',
						'	<div class="">' + v.lastWeek + '</div>',
						'	<div class="flex table_flex">',
						'		<p>' + v.thisWeek + '</p>',
						'		<img class="thisweek" data-thisweek="' +
							v.thisWeekState +
							'" src=""/>',
						'	</div>',
						'	<div class="flex table_flex">',
						'		<p>' + v.weekTarget + '</p>',
						'		<img class="weektargget" data-weektarget="' +
							v.weekTargetState +
							'"  src=""/>',
						'	</div>',
						'</li>',
					].join('')
					$(html).appendTo('.index_target_table_info')
				})
				$('.index_target_table_info li').each(function () {
					if (
						$(this).find('.index_target_table_info_name').attr('secname') !=
						'null'
					) {
						$(this)
							.find('.index_target_table_info_name')
							.append(
								'<span>' +
									$(this)
										.find('.index_target_table_info_name')
										.attr('secname') +
									'</span>'
							)
					}
					if ($(this).find('.thisweek').attr('data-thisweek') == 1) {
						$(this).find('.thisweek').attr('src', 'img/index/up.png')
					} else if ($(this).find('.thisweek').attr('data-thisweek') == 2) {
						$(this).find('.thisweek').attr('src', 'img/index/down.png')
					} else {
						$(this).find('.thisweek').remove()
					}
					if ($(this).find('.weektargget').attr('data-weektarget') == 1) {
						$(this).find('.weektargget').attr('src', 'img/index/bad.png')
					} else if (
						$(this).find('.weektargget').attr('data-weektarget') == 2
					) {
						$(this).find('.weektargget').attr('src', 'img/index/good.png')
					} else {
						$(this).find('.weektargget').remove()
					}
				})
			} else {
				$('.none_notice p').text(data.message)
			}
		},
		function (data) {
			console.log('error')
		}
	)
	//
})
